<?php

class Nodes extends Controller implements IExceptionHandler {

    public function ExceptionHandler(Exception $e) {
        $this->error = $e;
        if ($e instanceof AuthenticationException) {
            exit;
        } else {
            $this->view('main/index');
        }
    }

    private function Authenticate() {
        if (!in_array($this->user->get_id_profile_type(), [ PROFILE_SUPER_ADMINISTRATEUR, PROFILE_ADMINISTRATEUR])) {
            global $MESSAGE_0001;
            throw new AuthenticationException($MESSAGE_0001);
        }
    }

    public function GET_list() {
        $this->Authenticate();
        $this->view('main/nodes/list');
    }

    public function GET_create() {
        $this->Authenticate();
        $this->view('main/nodes/create');
    }

    public function POST_create() {
        $this->Authenticate();

        $PARAM_noeud_parent = lib_check_param_post($_POST['noeud_parent']);
        $PARAM_designation = lib_check_param_post($_POST['designation']);
        $PARAM_type = lib_check_param_post($_POST['type']);

        $tab_arg = array(':noeud_parent' => $PARAM_noeud_parent,
            ':designation' => $PARAM_designation,
            ':type' => $PARAM_type);

        $this->user->obj_Account->obj_Corporate->create_corporate_node($tab_arg);
        $this->view('main/index');
    }

    public function GET_update() {
        $this->Authenticate();
        $this->view('main/nodes/update');
    }

    public function POST_update() {
        $this->Authenticate();

        $PARAM_id_noeud = lib_check_param_post($_POST['id_noeud']);
//        $PARAM_noeud_parent = lib_check_param_post($_POST['noeud_parent_update']);
        $PARAM_designation = lib_check_param_post($_POST['designation_update']);
        $PARAM_type = lib_check_param_post($_POST['type_update']);

        // TODO
        // A CODER la mise à jour du noeud_parent
        $tab_arg = [
            ':id_noeud' => $PARAM_id_noeud,
            ':designation' => $PARAM_designation,
            ':type' => $PARAM_type
        ];

        $this->user->obj_Account->obj_Corporate->update_corporate_node($tab_arg);
        $this->user->obj_Account->obj_Corporate->hydrate();
        $this->view('main/index');
    }

    public function POST_delete($id) {
        $this->Authenticate();

        global $REQUEST_043, $REQUEST_044, $MESSAGE_0027, $MESSAGE_0028;

        $tab_arg = [':id_noeud' => $id];

        //On recherche si des noeud sont liés à ce noeud
        $data_0 = db_request_select($REQUEST_044, $tab_arg, $this->user->obj_Account->get_db_name());

        if ($data_0 != NULL && $data_0[0][0] != NULL) {
            throw new Exception($MESSAGE_0028);
        }

        //On recherche si des users sont liés à ce noeud
        $data_1 = db_request_select($REQUEST_043, $tab_arg, $this->user->obj_Account->get_db_name());

        if ($data_1 != NULL && $data_1[0][0] != NULL) {
            throw new Exception($MESSAGE_0027);
        }

        $this->user->obj_Account->obj_Corporate->delete_corporate_node($tab_arg);
        $this->view('main/index');
    }

}
